#include "AiBase.h"
#include "TiledMapMgr.h"
#include "GameScene.h"

CAiBase::CAiBase()
{
	
}

CAiBase::~CAiBase()
{
}

/// <summary>
/// Initializes the specified enemy.
/// </summary>
/// <param name="enemy">The enemy.</param>
void CAiBase::init(CEnemy* enemy)
{
	m_fActionInterval = 0;
	m_pAiStatus = AI_IDLE;
	m_bIsWalking = false;
	m_pEnemy = enemy;
	m_nAiLevel = enemy->getAILevel();
}



void CAiBase::ai()
{

	switch (m_pAiStatus)
	{
	case AI_IDLE:
		idle();
		break;
	case AI_WALK:
		if (m_bIsWalking)
			return;
		m_fActionInterval = (rand() % m_nAiLevel + 0.2) * 30;
		move();
		break;
	case AI_ATTACK:
		m_fActionInterval = (rand() % m_nAiLevel + 0.2) * 30;
		attack();
		break;
	case AI_SKILL:
		m_fActionInterval = (rand() % m_nAiLevel + 0.2) * 30; 
		skill();
		break;
	default:
		break;
	}
}

void CAiBase::move()
{
	
	CTiledMapMgr* tieldMgr = CTiledMapMgr::getInstance();
	Vec2 tiledPos = tieldMgr->getTiledPosByGamePos(m_pEnemy->getPosition());
	CPoint enemyPoint(tiledPos.x, tiledPos.y);

	Vec2 playerPos = CGameScene::getInstance()->getPlayer()->getCurTiledMap();
	CPoint playerPoint(playerPos.x, playerPos.y);
	
	Cplayer* m_pPlayer = CGameScene::getInstance()->getPlayer();
	Vec2 m_playerPos = m_pPlayer->getPosition();
	if (abs(m_playerPos.distance(m_pEnemy->getPosition()) < 40)){
		m_bIsWalking = false;
		m_pAiStatus = AI_ATTACK;
		return;
	}

	Astar* m_pAstar = new Astar();
	m_pAstar->initFun(CC_CALLBACK_1(CAiBase::isCollideObject, this));
	m_path = m_pAstar->GetPath(enemyPoint, playerPoint, true);
	if (!m_path.empty())
	{
		iter = m_path.begin();
		runPath();
		m_pEnemy->replaceAnimation(MOVE);
	}
}


bool CAiBase::isCollideObject(Vec2 pos)
{
	return CTiledMapMgr::getInstance()->isCollideObjectByTiled(pos);
}

//void CAiBase::attack()
//{
	//else if (m_pDt->strSort == "boss"){
	//	for (int i = 0; i < 12; i++){
	//		float temp = 3.14 / 6;
	//		m_pBulletMgr->instanBullet(3003, 0.1 + i * temp, pos, false);
	//	}
	//	m_pWeaponPic->stopAllActions();
	//	CUtils::getInstance()->runAnimationOnce(m_pWeaponPic, "enemyBornEffect");
	//	scheduleOnce([&](float)->void{CUtils::getInstance()->runAnimation(m_pWeaponPic, "weapon"); }, 0.5, "weapon");
	//	CGameScene::getInstance()->getEnemyMgr()->initEnemy(2002, 2, this->getPosition());
	//}
	//else if (m_pDt->strSort == "caster"){
	//	m_pBulletMgr->instanBullet(3006, radia, pos, false);
	//	scheduleOnce([&](float)->void{this->attack(); }, 0.6, "attack");
	//}

	//this->scheduleOnce([&](float)->void{m_fActionInterval = 0; }, m_fActionInterval, "action");
//}



float CAiBase::getRotation()
{
	//获取玩家和敌人之间的角度
	Vec2 playerPos = CGameScene::getInstance()->getPlayer()->getPosition();
	Vec2 enemyPos = m_pEnemy->getPosition();
	float offsetY = playerPos.y - enemyPos.y;
	float offsetX = playerPos.x - enemyPos.x;
	float radia = atan2(offsetY, offsetX);
	return radia;
}

void CAiBase::idle()
{
	m_pEnemy->replaceAnimation(IDLE);
	if (m_fActionInterval > 0){
		--m_fActionInterval;
	}
	else{
		m_pAiStatus = AI_WALK;
	}
}


